
#include "authorization.h"
#include "pt_fs.h"
#include "first_title.h"

ui_authorization_widget_t u_auth_w = {
    .loading_img_count = 0,
    .img_loading       = NULL,
    .img_loading_timer = NULL,
};

void authorization_loading_img_cb(lv_timer_t * t)
{
    u_auth_w.loading_img_count++;    
    if(u_auth_w.loading_img_count > 17){
        u_auth_w.loading_img_count = 0;
    }

    midea_ui_set_img_src(u_auth_w.img_loading,loading_img[u_auth_w.loading_img_count]);
    //************************************************** */
#if 1 //for test
    static int test_cnt = 0;
    test_cnt++;
    if(test_cnt > 25) {
        test_cnt = 0;
    }else{
        return ;
    }

    pt_accredit_set(1); 
#endif
    //************************************************** */
    if(pt_accredit_get()){
        if(u_auth_w.img_loading_timer){
            midea_ui_safe_del_timer(u_auth_w.img_loading_timer);
            u_auth_w.img_loading_timer = NULL;
        }

        debug_d("link net\n");

        page_install("link_net",lv_page_linked_network_create(),NULL);
        page_push_anim("link_net",LOAD_ANIM_MOVE_LEFT);
    }
    else{
         debug_d("link err\n");

        page_install("link_fail",lv_page_linked_network_fail_create(),NULL);
        page_push_anim("link_fail",LOAD_ANIM_MOVE_LEFT);
    }
    //************************************************** */
}

void ui_init_authorization_page_src(lv_obj_t *superview)
{
    // first_title_set( FIRST_PAGE_AUTHORIZATION, 0, 4);

    u_auth_w.img_loading = lv_img_create(superview);
    midea_ui_set_img_src( u_auth_w.img_loading, loading_img[0]);
    midea_ui_safe_obj_align( u_auth_w.img_loading,LV_ALIGN_TOP_LEFT,196,111);
 
    lv_obj_t * label = lv_label_create(superview);
    lv_obj_set_style_text_font( label, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(label,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(label,LV_OPA_80,0);
    midea_ui_safe_set_text( label, "正在等待授权...");
    lv_obj_set_pos(label,154,176);

   u_auth_w.img_loading_timer = lv_timer_create(authorization_loading_img_cb, 50, NULL);
}

void ui_release_authorization_page_src(lv_obj_t *superview)
{
    if(u_auth_w.img_loading)
    {
        midea_ui_safe_del_obj(u_auth_w.img_loading);
        u_auth_w.img_loading = NULL;
    }

    if(u_auth_w.img_loading_timer)
    {
        midea_ui_safe_del_timer(u_auth_w.img_loading_timer);
        u_auth_w.img_loading_timer = NULL;
        u_auth_w.loading_img_count = 0;
    }
}

static void authorization_page_on_custom_attr_config(page_base_t* self)
{
    return ;
}
static void authorization_page_on_view_load(page_base_t* self)
{
    ui_init_authorization_page_src(self->root);
    return ;
}
static void authorization_page_on_view_did_load(page_base_t* self)
{
    return ;
}
static void authorization_page_on_view_will_appear(page_base_t* self)
{
    return ;
}
static void authorization_page_on_view_did_appear(page_base_t* self)
{
    return ;
}
static void authorization_page_on_view_will_disappear(page_base_t* self)
{
    return ;
}
static void authorization_page_on_view_did_disappear(page_base_t* self)
{
    return ;
}
static void authorization_page_on_view_did_unload(page_base_t* self)
{
    ui_release_authorization_page_src(self->root);
    return ;
}
static page_vtable_t page_param = {
    .on_custom_attr_config  = authorization_page_on_custom_attr_config,
    .on_view_load           = authorization_page_on_view_load,
    .on_view_did_load       = authorization_page_on_view_did_load,
    .on_view_will_appear    = authorization_page_on_view_will_appear,
    .on_view_did_appear     = authorization_page_on_view_did_appear,
    .on_view_will_disappear = authorization_page_on_view_will_disappear,
    .on_view_did_disappear  = authorization_page_on_view_did_disappear,
    .on_view_did_unload     = authorization_page_on_view_did_unload,
};


page_vtable_t* lv_page_authorization_create(void)
{
    return &page_param;
}
